package prictise.com.application1.cusview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CusTextView extends View {
  float p = 0;

  public float getP() {
    return p;
  }

  public void setP(float p) {
    this.p = p;
    invalidate();
  }

  public void setR(float p) {

  }
  Paint paint;
  String content = "欢迎观看";

  public CusTextView(Context context) {
    super(context);
  }

  public CusTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
  }

  public CusTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onDraw(Canvas canvas) {

    paint.setColor(Color.RED);
    canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, paint);
    canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), paint);

    paint.setTextSize(70);
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    float top = fontMetrics.top;
    float bottom = fontMetrics.bottom;
    float y = (bottom - top) / 2;

    canvas.save();
    paint.setColor(Color.BLACK);
    paint.setTextAlign(Paint.Align.CENTER);
    float textWidth1 = paint.measureText(content);
    float rLeft1 = (getWidth() / 2 - textWidth1 / 2);
    float rRight1 = rLeft1 + textWidth1;
    float rLeft2 = rLeft1 + p * textWidth1;
    canvas.clipRect(new Rect((int) rLeft2, 0, (int) rRight1, getHeight()));
    canvas.drawText(content, getWidth() / 2, getHeight() / 2 + y - bottom, paint);
    canvas.restore();

    /*canvas.save();
    paint.setColor(Color.BLACK);
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(content, getWidth() / 2, getHeight() / 2 + y - bottom + 300, paint);
    canvas.restore();*/


    canvas.save();
    float textWidth = paint.measureText(content);
    int rLeft = (int) (getWidth() / 2 - textWidth / 2);

    paint.setColor(Color.RED);
    paint.setTextAlign(Paint.Align.CENTER);
    float rRight = rLeft + p * textWidth;
    canvas.clipRect(new Rect(rLeft, 0, (int) rRight, getHeight()));
    canvas.drawText(content, getWidth() / 2, getHeight() / 2 + y - bottom, paint);

    paint.setColor(Color.BLACK);
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(content, getWidth() / 2, getHeight() / 2 + y - bottom + 300, paint);

    canvas.restore();
  }
}
